﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CleanSVG
{
    public partial class OptionsForm : Form
    {
        public OptionsForm(SVGCleanOptions options)
        {
            InitializeComponent();

            InitOptions(options);
        }

        private void InitOptions(SVGCleanOptions options)
        {
            var optionsType = options.GetType();
            var optionsFields = optionsType.GetFields();

            foreach (var info in optionsFields)
            {
                if (info.FieldType == typeof(bool))
                {
                    optionsListBox.Items.Add(Regex.Replace(info.Name, @"([a-z])([A-Z])", "$1 $2"),
                        (bool)optionsType.GetField(info.Name).GetValue(options));
                }
            }

            digitsPrecision.Value = options.DigitsOfFloatingPointPrecision;
            EnableDisableDigitsOfPrecision();
        }

        public void SaveOptions(SVGCleanOptions options)
        {
            var optionsType = options.GetType();

            for (var i = 0; i < optionsListBox.Items.Count; ++i)
            {
                var fieldName = ((string)optionsListBox.Items[i]).Replace(" ", string.Empty);
                optionsType.GetField(fieldName).SetValue(options, optionsListBox.GetItemChecked(i));
            }

            options.DigitsOfFloatingPointPrecision = Convert.ToInt32(digitsPrecision.Value);
        }

        private void optionsListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            EnableDisableDigitsOfPrecision();
        }

        private void EnableDisableDigitsOfPrecision()
        {
            var i = optionsListBox.Items.IndexOf("Reduce Floating Point Precision");

            if (i != -1)
                digitsLabel1.Enabled =   digitsPrecision.Enabled = optionsListBox.GetItemChecked(i);
        }
    }
}
